﻿using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.SDK;
using Bentley.TerrainModelNET;
namespace TemplateStandardLibrary.src.Operation.LD
{
    class TerrainObj
    {

        public CurveVector DropLineToDTM(List<DPoint3d> dPoints, DVector3d dVector3D)
        {
            DTM dtm = GetDTMElement_ORD();

            if (dtm == null)
            {
                dtm = GetDTMElement_CE();
            }
            if (dtm == null)
            {
                return null;
            }
            CurveVector curves = null;
            Bentley.TerrainModelNET.DTMMeshEnumerator polyfaces1 = new Bentley.TerrainModelNET.DTMMeshEnumerator(dtm);
            foreach (PolyfaceHeader polyface in polyfaces1)
            {
                curves= polyface.DrapeLinestring(dPoints, dVector3D);
                if (curves != null) break;
            }
            return curves;
        }

        /// <summary>
        /// 获取ORD创建的地形
        /// </summary>
        public DTM GetDTMElement_ORD()
        {
            ConsensusConnection sdkCon = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
            if (sdkCon == null)
                return null;
            GeometricModel geomModel = sdkCon.GetActiveGeometricModel();
            if (geomModel !=null)
            {
                SurfaceEntity activeSurface = geomModel.ActiveSurface;
                if (activeSurface !=null)
                {
                    TerrainSurface surface = activeSurface as TerrainSurface;
                    DTM dtm = surface.DTM;
                    return dtm;
                }
            }
            return null;
        }

        public DTM GetDTMElement_CE()
        {
            ModelElementsCollection cols = Session.Instance.GetActiveDgnModel().GetElements();
            foreach (var element in cols)
            {
                if (element is Bentley.TerrainModelNET.Element.DTMElement)
                {
                    Bentley.TerrainModelNET.Element.DTMElement dtmElement = element as Bentley.TerrainModelNET.Element.DTMElement;
                    Bentley.TerrainModelNET.DTM dtm = dtmElement.GetDTM();
                    if (dtm != null)
                    {
                        return dtm;
                    }
                }
            }
            return null;
        }
    }
}
